package com.whut.blog.utils;


import java.io.IOException;  
import java.nio.file.Files;  
import java.nio.file.Path;  
import java.nio.file.Paths;  
import java.util.Base64;  
  
public class ImageToBase64 {  
  
    public static String encodeImageToBase64(String imagePath) throws IOException {  
        Path path = Paths.get(imagePath);  
        byte[] imageBytes = Files.readAllBytes(path);  
        String encodedString = Base64.getEncoder().encodeToString(imageBytes);  
        return "data:image/jpeg;base64,"+encodedString;  
    }  
  
    public static void main(String[] args) {  
        String imagePath = "img/2.png"; // 替换为你的图片路径  
        try {  
            String base64String = encodeImageToBase64(imagePath);  
            System.out.println(base64String);  
        } catch (IOException e) {  
            e.printStackTrace();  
        }  
    }  
}